# -*- coding: utf-8 -*-
"""
@Time ： 2021/11/30 16:57
@Auth ： 颜
@File ：excle的应用.py
@IDE ：PyCharm
"""
import xlwt

#
# workbook = xlwt.Workbook(encoding='utf-8')  # 创建对象
# worksheet = workbook.add_sheet('sheet1')  # 创建工作表
# # 把九九乘法表写入到excel中
# for i in range(1, 10):
#     for j in range(1, i + 1):
#         # i是行j是列 后面的字符串是内容
#         worksheet.write(i, j, "{}*{}={}".format(j, i, i * j))
# workbook.save('student.xls')
# datadict = {"学生": ['小红', '小明', '小花'], '年龄': [20, 18, 17], '性别': ['女', '男', '女']}
# a = ()
# a_lsit = list(a)
# for key in datadict.keys():
#     a_lsit.append(key)
# a = tuple(a_lsit)
# print(type(a[1]))
# data = key
...
# p = data
# data = ("xiaoming", '雄安红')
# print(data[1])
# print(len(key))
# data = tuple([key for key in datadict])
# new_data = [v for v in datadict.values()]
# # print(new_data[1][1])
# workbook = xlwt.Workbook(encoding='utf8', style_compression=0)
# sheet = workbook.add_sheet("学生", cell_overwrite_ok=True)
# for i in range(0, len(data)):
#     sheet.write(0, i, data[i])
# for i in range(0, len(new_data)):
#     data_old = new_data[i]
#     for j in range(0, len(data)):
#         sheet.write(j+1, i, data_old[j])
# workbook.save('测试.xls')
#
#     print(new_data)
#     print(len(data))
# data = {'name': '小明', "age": "18", "sex": "女"}
# old_data = [key for key in data.keys()]
# old_value = [v for v in data.values()]
# for key, value in data.items():
#     data1 = tuple(key)
# work_book = xlwt.Workbook(encoding='utf8', style_compression=0)
# sheet = work_book.add_sheet("测试2", cell_overwrite_ok=True)
#
# for i in range(0, len(old_data)):
#     sheet.write(0, i, old_data[i])
# for j in range(0, len(old_value)):
#     sheet.write(1, j, old_value[j])
# work_book.save('测试2.xls')
# new_data = [(key, v) for key, v in data.items()]
# print(new_data[-1:][0])
# for j in data.values():
#     print(j)
# datalist = []
# new_data = []
# name = '小红'
# datalist.append(name)
# name1 = '小白'
# datalist.append(name1)
# age = '20'
# datalist.append(age)
# age1 = '21'
# datalist.append(age1)
# sex = '男'
# datalist.append(sex)
# sex1 = '男'
# datalist.append(sex1)
# addr = '成都'
# datalist.append(addr)
# addr1 = '上海'
# datalist.append(addr1)
# new_data.append(datalist)
# print(new_data)
# work_book = xlwt.Workbook(encoding='utf8', style_compression=0)
# work_sheet = work_book.add_sheet('测试3', cell_overwrite_ok=True)
# col = ('姓名', '年龄', '性别', '住址')
# for i in range(0, len(col)):
#     work_sheet.write(0, i, col[i])
# for j in range(1, len(new_data)):
#     print(len(new_data))
#     data = new_data[j]
#     for i in range(0, len(col)):
#         work_sheet.write(j + 1, i, data[i])
# work_book.save('测试3.xls')
from openpyxl import Workbook
from openpyxl.utils import get_column_letter

wb = Workbook()
ws = wb.active
# tableTitle = ['userName', 'Phone', 'age', 'Remark']
# cols = ["详情页链接", '电影名', '别名', '电影导演和主演', '评分', '评价人数', '介绍', '图片链接']
cols = ['姓名', '年龄', '性别', '住址']
datalsit = [['小明', 20, '男', '成都市'], ['小红', 21, '女', '上海市']]

# 维护表头
#        if row < 1 or column < 1:
#          raise ValueError("Row or column values must be at least 1")
# 如上，openpyxl 的首行、首列 是 （1,1）而不是（0,0），如果坐标输入含有小于1的值，提示 ：Row or column values must be at least 1，即最小值为1.
for col in range(len(cols)):
    c = col + 1
    ws.cell(row=1, column=c).value = cols[col]
for row in range(0, len(datalsit)):
    data = datalsit[row]
    for j in range(0, len(cols)):
        ws.cell(row=row + 2, column=j + 1).value = data[j]

# ws.title = 'rang_temp'
# col = ["详情页链接", '电影名', '别名', '电影导演和主演', '评分', '评价人数', '介绍', '图片链接']
# data = ['小明', '小红', '大明']
# for i in range(len(col)):
#     c = col + 1
#     ws.cell(row=1, column=c, value=col[i])
# for j in range(1, len(data)):
#     ws.cell(row=i, column=j, value=col[i])
wb.save(filename='new_rang.xlsx')
# wb = Workbook()
#
# dest_filename = 'empty_book.xlsx'
#
# ws1 = wb.active
# ws1.title = "range names"
#
# for row in range(1, 40):
#     ws1.append(range(600))
#
# ws2 = wb.create_sheet(title="Pi")
#
# ws2['F5'] = 3.14
#
# ws3 = wb.create_sheet(title="Data")
# for row in range(10, 20):
#     for col in range(27, 54):
#         ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
# print(ws3['AA10'].value)
# wb.save(filename=dest_filename)
